package com.maxjay.component.metadata.support;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.maxjay.common.exception.ERR;
import com.maxjay.common.exception.GeneException;

@SuppressWarnings({"rawtypes", "unchecked"})
public class ParamStringFormat {
	
	private static Logger logger = Logger.getLogger(ParamStringFormat.class);
	
	/**
	 * 获取字符串中的所有参数名
	 * 
	 * @param paramStr
	 * @return
	 */
	public static Set<String> parse(String paramStr){
		Set<String> params = new HashSet<String>(0);
		if(StringUtils.isNotBlank(paramStr)){
			Pattern pattern = Pattern.compile("\\$\\{[^\\}]+\\}");
			Matcher matcher = pattern.matcher(paramStr);
			try {
				while(matcher.find()){
					String group = matcher.group();
					String param = group.substring(2, group.length()-1);
					params.add(param);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw GeneException.getGeneException(ERR.ERROR_ERR_MSG, e);
			}
		}
		return params;
	}
	
	public static String format(String paramStr, Map paramMap) {
		return format(paramStr, paramMap, null);
	}
	
	/**
	 * 取得替换参数后的url字符串
	 * 
	 * @param url 格式为：finance/consumption.do?id=%{ID}&backUrl=%{currentUrl}
	 * @param paramMap 包含url中所使用参数的map
	 * @param source url中可以使用source中的属性，可以用objA.objB.name格式
	 * @return
	 */
	public static String format(String url, Map<String, ?> paramMap, Object source){
		String formatUrl = url;
		Set<String> params = ParamStringFormat.parse(url);
		try {
			for (String param : params) {
				Object fieldValue = "";
				if (!paramMap.keySet().contains(param.toUpperCase())) {
					fieldValue = source;
					for (String splitParam : param.split("\\.")) {
						if (fieldValue != null) {
							try {
								fieldValue = PropertyUtils.getProperty(fieldValue, splitParam);
							} catch (NoSuchMethodException e) {
								fieldValue = "";
								e.printStackTrace();
								logger.error("无法取得URL:" + url + "中的参数:" + param + "的值");
								break;
							}
						} else {
							fieldValue = "";
							logger.error("无法取得URL:" + url + "中的参数:" + param + "的值");
							break;
						}
					}
				} else {
					if(paramMap.get(param.toUpperCase()) != null){
						fieldValue = String.valueOf(paramMap.get(param.toUpperCase()));
					}
				}
				formatUrl = formatUrl.replaceAll("\\$\\{" + param + "\\}", fieldValue.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw GeneException.getGeneException(ERR.ERROR_ERR_MSG, e);
		}
		return formatUrl;
	}
	
	public static void main(String[] args) throws Exception {
//		String url = "commonSimpleList.do?listId=PROJECT_VERSION_LOG&query_param_version_id_eq=${VERSION_ID}&page.pageIndex=${page.pageIndex}";
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("VERSION_ID", "2000");
//		SimpleListController controller = new CommonSimpleListController();
//		controller.getPage().setPageIndex("3");
//		System.out.println(PropertyUtils.getProperty(controller, "page"));
//		System.out.println(format(url, map, controller));
		
	}
}
